<?php 
$o_Bean= $a_Maestros['o_ArFacturaBN']
?>


<body>
    
<script>

    var iCorrelativoRecep = 0;

    add_TR_TbodyRecep = function ()
    {

      iCorrelativoRecep = iCorrelativoRecep+1

      var id_ItemCode = "itemCode_"+iCorrelativoRecep;

      var sTr='';
         sTr+="<tr bgcolor='#FFFFFF'>";
         sTr+="<td ><input type='text' itemG='itemG' id='"+id_ItemCode+"' name='a_ItemId["+iCorrelativoRecep+"]' size='15' style='width: 100px;' class='loadF2 required' ></td>";
         sTr+="<td id='txtDesc' desc='desc'>&nbsp;</td>";
         sTr+="<td UoM='UoM'>&nbsp;</td>";
         sTr+="<td ><input type='text' cant='cant' name='a_Cant["+iCorrelativoRecep+"]' size='5' class='number required' style='text-align:right' ></td>";
         sTr+="<td ><input type='text' precio='precio' name='a_Precio["+iCorrelativoRecep+"]' size='5' class='number required' style='text-align:right' ></td>";
         sTr+="<td monto='monto' align='right' >&nbsp;</td>";
        sTr+="<td align='right'><img border='0' style='cursor:pointer' title='Eliminar Registro' src='/gourmet-express/trunk/imgs/icons/delete_icon.png' onclick='delete_TR_TbodyRecep(this)'></td>"; 		
        sTr+="</tr>";

      $("#tableNuevoRecep tbody").append(sTr);

      loadAutocompleteItem(id_ItemCode);

        cantKeyupEvent();
        costKeyupEvent();
    }

    delete_TR_TbodyRecep = function (obj)
    {
     $(obj).parents('tr').first().remove();
     evalMontos();

    }  


    cantKeyupEvent = function()
    {
      $("#tableNuevoRecep tbody tr td input[cant='cant']").keyup(function(){

      var objKey = $(this).parent().parent();
      evalCosto(objKey);


      })
    };

    costKeyupEvent = function()
    {
      $("#tableNuevoRecep tbody tr td input[precio='precio']").keyup(function(){

      var objKey = $(this).parent().parent();
      evalCosto(objKey);


      })
    };

    evalCosto = function (objKey)
    {
      var s_cant = $(objKey).children('td').children("input[cant='cant']").val();
      var s_cost = $(objKey).children('td').children("input[precio='precio']").val();

      if(s_cant && s_cost)
      {
        var montoTotReg = parseInt(s_cant)*parseFloat(s_cost);
        $(objKey).children("td[monto='monto']").text(montoTotReg);
      }else{
        $(objKey).children("td[monto='monto']").text('');
      }

      evalMontos();

    }

    evalMontos = function ()
    {

      totMont = 0;
      totImp  = 0;
      totNeto = 0;

      $("td[monto='monto']").each(function() { 
                if(!isNaN($(this).text()) && $(this).text().length!=0) {
                    totMont += parseFloat($(this).text());
                }          
      });

      totImp  = totMont * 0.18;
      totNeto = totMont + totImp;

      $("#totalMonto").html(totMont.toFixed(2));
      $("#totalImp").html(totImp.toFixed(2));
      $("#totalNeto").html(totNeto.toFixed(2));
    }
</script>


<div id="main">
  <div id="zonaContenedor">
    <div id="module">
      <h3>:: [Editar]So Factura </h3>
      <div>
          <form id="form_editar" action="javascript:fn_actualizar();">
          
            <input type='hidden' id='facId' name='facId' value="<?php echo $o_Bean->getFacId();?>">
            <table width="100%">
            	<tr>
                    <td id="keyLabel" width="20%">Id</td>
                    <td width="30%"><?php echo $o_Bean->getFacId();?></td>
                    <td id="label">Estado</td>
                    <td><select name="estado" id="estado">
                    <option>-- seleccione --</option><?php
                    foreach ($a_Maestros['estadoFactura'] as $key => $obj){?>
                        <option value="<?php echo $obj->getValorId();?>" <?php if($o_Bean->getStatus() == $obj->getValorId()){?> selected<?php }?>><?php echo $obj->getDescripcion();?></option><?php
                        }?>
                        </select>
                    </td>
              </tr>
              <tr>
                <td id="label">Vendedor</td>
            	<td><input name="vendedorId" id='vendedorId' type="text" size="15" value="<?php echo $o_Bean->getVendedorId();?>"></td>
                <td id="label">Factura Nro</td>
                <td><input type="text" name="facNro" id="facNro" size="14" value="<?php echo $o_Bean->getNum();?>"/></td>
              </tr>             	
            	<tr>
            	  <td id="label">Cash / Cred</td>
            	  <td>
            			<select name="pagVts" id="PagVts">
                      <option value=''>-- seleccione --</option><?php
                      foreach ($a_Maestros['PagVts'] as $key => $obj){?>
                      <option value="<?php echo $obj->getValorId();?>" <?php if($o_Bean->getTerms() == $obj->getValorId()){?> selected<?php }?> ><?php echo $obj->getDescripcion();?></option><?php
                      }?>
                  </select>
            	  </td>
            	  <td id="label">Doc Ref</td>
            	  <td><input type="text" name="docRef" id="docRef" size="12" value="<?php echo $o_Bean->getDocReferNum();?>" class="loadF2_OrdVta" /></td>
              </tr>                 
              <tr>
                <td id="label">Cliente</td>
                <td nowrap><input name="clienteId" type="text" id="clienteId" value="<?php echo $o_Bean->getClienteId();?>" size="12"  style="background-color:#F0F0F0" />
                  <input name="clienteNombre" type="text" id="clienteNombre" value="<?php echo $o_Bean->getClienteNombre();?>" size="40" maxlength="40"/></td>
                <td id="label">Fecha Fact</td>
                <td><input type="text" name="fechaReg" id="fechaReg"  size="12" value="<?php echo $o_Bean->getFecha();?>"/></td>
              </tr>
            	<tr>
            	  <td id="label">Contacto</td>
            	  <td><input name="contacto" type="text" id="contacto" size="40" maxlength="40" value="<?php echo $o_Bean->getContacto();?>"/></td>
            	  <td id="label">Fecha Vcto</td>
            	  <td><input type="text" name="fechaVcto" id="fechaVcto" size="12" value="<?php echo $o_Bean->getFechaVcto();?>"/></td>
            	  </tr>
            	<tr>
                <td id="label">Observaciones</td>
                <td>
                <textarea name="obs" cols="40" rows="2" id='obs'><?php echo $o_Bean->getObs();?></textarea></td>
                <td id="label" valign="top">Moneda</td>
                <td valign="top">
                  <select name="moneda" id="moneda">
                    <option value="">-- seleccione --</option><?php
                      foreach ($a_Maestros['Moneda'] as $key => $obj){?>
                      <option value="<?php echo $obj->getValorId();?>" <?php if($o_Bean->getMoneda() == $obj->getValorId()){?> selected<?php }?> ><?php echo $obj->getDescripcion();?></option><?php
                      }?>
                  </select>
                </td>
              </tr>        
              <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
              </tr>
            </table>
            
            <table width=100%>
            	<tr>
            		<td>
            			<table cellspacing="1" cellpadding="4">
            				<tr>
            					<td id="tabFormula" bgcolor="#CCCCCC" onclick="showGrid('gridFormula')"><a>Detalle</a></td>
            					<td id="tabFase" bgcolor="#EBEBEB" onclick="showGrid('gridFase')">Condiciones comerciales  </td>                
            				</tr>
            			</table>        					
            		</td>
            		<td align=right>
                  <a href="javascript:add_TR_TbodyRecep();">Agregar detalle</a>
            		</td>
            	</tr>
            </table>
            
            <table width="100%" >
              <tr>
                <td colspan="4">
                  <table width="100%" cellspacing="1" id='tableNuevoRecep'>
                    <thead>
                      <tr bgcolor="#CCCCCC" >
                        <td>Codigo</td>
                        <td width="35%">Descripcion</td>
                        <td>UM</td>
                        <td>Cantidad</td>
                        <td>Precio</td>
                        <td>Subtotal</td>
                        <td></td>                                 
                      </tr>
                    </thead>
                    <tbody><?php
                    if($o_Bean->getObj_ArFacturaLinea())
                    {
                        $correlaTivo = 1;
                        
                        foreach ( $o_Bean->getObj_ArFacturaLinea() as $iKey => $objRcp )
                        {
                          $id_ItemCode = "itemCode_".$correlaTivo;?>
                          <tr bgcolor='#FFFFFF'>
                          <td ><input type='text' itemG='itemG' id='<?php echo $id_ItemCode;?>' name='a_ItemId[<?php echo $correlaTivo;?>]' value="<?php echo $objRcp->getArtId() ;?>" size='15' style='width: 100px;' class='loadF2 required' ></td>
                          <td id='txtDesc' desc='desc'><?php echo $objRcp->get_oDetalleItemBean()->getDescripcion();?></td>
                          <td UoM='UoM' ><?php echo $objRcp->get_oDetalleItemBean()->getUndMedida();?></td>
                          <td ><input type='text' cant='cant' name='a_Cant[<?php echo $correlaTivo;?>]' size='5' value="<?php echo $objRcp->getCant();?>" class='number required' style='text-align:right' ></td>
                          <td ><input type='text' precio='precio' name='a_Precio[<?php echo $correlaTivo;?>]' size='5' value="<?php echo $objRcp->getPrecio();?>"  class='number required' style='text-align:right' ></td>
                          <td monto='monto' align='right' ><?php echo ( $objRcp->getCant()* $objRcp->getPrecio()) ;?></td>
                          <td align='right'><img border='0' style='cursor:pointer' title='Eliminar Registro' src='/gourmet-express/trunk/imgs/icons/delete_icon.png' onclick='delete_TR_TbodyRecep(this)'></td>
                          </tr>
                          <script> loadAutocompleteItem('<?php echo $id_ItemCode; ?>'); iCorrelativoRecep=<?php echo $correlaTivo;?>;</script><?php
                          
                          $correlaTivo++;
                        
                        }
                    }?>
                            
                    </tbody>
                    <tfoot>
                      <tr bgcolor="#FFFFFF">
                        <td colspan="3" bgcolor="#FFFFFF"><div align="right"></div></td>
                        <td colspan="2" bgcolor="#CCCCCC"><div align="right" >Total</div></td>
                        <td bgcolor="#CCCCCC"><div align="right" id='totalMonto'></div></td>
                        <td bgcolor="#CCCCCC"><div align="right"></div></td>            
                      </tr>
                      <tr bgcolor="#FFFFFF">
                        <td colspan="3" bgcolor="#FFFFFF"><div align="right"></div></td>
                        <td colspan="2" bgcolor="#CCCCCC"><div align="right">Descuentos</div></td>
                        <td bgcolor="#CCCCCC"><div align="right" ></div></td>
                        <td bgcolor="#CCCCCC"><div align="right"></div></td>
                      </tr>
                      <tr bgcolor="#FFFFFF">
                        <td colspan="3" bgcolor="#FFFFFF"><div align="right"></div></td>            
                        <td colspan="2" bgcolor="#CCCCCC"><div align="right">Impuestos</div></td>
                        <td bgcolor="#CCCCCC"><div align="right" id='totalImp' ></div></td>
                        <td bgcolor="#CCCCCC"><div align="right"></div></td>
                      </tr>
                      <tr bgcolor="#FFFFFF">
                        <td colspan="3" bgcolor="#FFFFFF"><div align="right"></div></td>
                        <td colspan="2" bgcolor="#CCCCCC"><div align="right"><strong>Neto</strong></div></td>
                        <td bgcolor="#CCCCCC" align='right'><div align="right" id='totalNeto'></div></td>
                        <td bgcolor="#CCCCCC"><div align="right"><strong></strong></div></td>            
                      </tr>
                    </tfoot>																		
            	    </table>
                </td>
              </tr>
            </table>
   
            <table width="100%">
              <tr>
                <td width="90%" align="right"><input type="submit" name="guardar" value="Guardar"></td>
                <td align="right"><input type="button" id="btn_cancelar"name="btn_cancelar" onclick="javascript: listarSoFactura()" value="Cancelar"></td>
              <tr>	
            </table> 
            <br>&nbsp;
            <div id="moduleSectionDetalleAdd"> </div>
          </form>
      </div>
      <div id="gridFase" style="top:237px"></div>
    </div>
    <div id="footer"></div>
    <div id="moduleSectionDetalleAddList"> </div>
  </div>   
</div> 

<script>
    cantKeyupEvent();
    costKeyupEvent();
    evalMontos();

    $("#form_editar").validate({
              rules: {
                   clienteId: "required",
                   facNro: "required",
                   fechaReg: "required",
                   contacto: "required",
                   moneda:"required",
                   fechaVcto : "required"
              },
              messages: {
                   clienteId: "Ingrese Cod Proveedor",
                   facNro: "Ingrese Nro de Factura",
                   fechaReg: "Ingrese Fecha de Registro",
                   contacto: "Ingrese Contacto",
                   docRef: "Ingrese Doc Ref",
                   moneda: "Seleccione Moneda",
                   fechaVcto : "Ingrese Fecha de Vencimiento"
              }
    });

    $(function() {
        $("#fechaReg").datepicker( {dateFormat :"yy-mm-dd", defaultDate: "<?php echo $o_Bean->getFecha();?>" });
        $("#fechaVcto").datepicker( {dateFormat :"yy-mm-dd", defaultDate: "<?php echo $o_Bean->getFechaVcto();?>" });

    });

    loadAutocompleteCliente('clienteNombre');
</script>
</body>
</html>
